<?php
class database{
private $host="localhost";
private $user_host="root";
private $pass_host="root";
private $dbname="nhtuna8s_ducngoco2";
private $conn;
private $result;

public function connect(){
	$this->conn=mysql_connect($this->host,$this->user_host,$this->pass_host);
	mysql_select_db($this->dbname,$this->conn);
}

public function disconnect(){
	if($this->conn){
		mysql_close($this->conn);
	}
}

public function query($sql){
	$this->result=mysql_query($sql);
}

public function num_rows(){
	if($this->result){
		$row=mysql_num_rows($this->result);
	}else{
		$row=0;
	}
	return $row;
}

public function fetch(){
	if($this->result){
		$row=mysql_fetch_assoc($this->result);
	}else{
		$row=0;
	}
	return $row;
}

public function result_array(){
	while($data=$this->fetch()){
		$row[]=$data;
	}
	return $row;
}
	
}

class Model extends database{

private $select;
private $where;
private $order;
private $limit;

public function __construct(){
	$this->connect();
}	

public function insert($table,$data){
	$col=implode(",",array_keys($data));
	$arr=array_values($data);
	foreach($arr as $arr2){
		$arr3[]="'".$arr2."'";
	}
	$value=implode(",",$arr3);
	$sql="insert into $table($col) values($value)";
	$this->query($sql);
}

public function update($table,$data){
	foreach($data as $k=>$v){
		$arr[]="$k='$v'";
	}
	$col=implode(",",$arr);
	if($this->where != ""){
		$where="where ".$this->where;
	}
	$sql="update $table set $col $where";
	$this->query($sql);
}

public function delete($table){
	if($this->where != ""){
		$where="where ".$this->where;
	}	
	$sql="delete from $table $where";
	$this->query($sql);
}

public function select($str){
	$this->select=$str;
}

public function getdata($table){
	if($this->select != ""){
		$col=$this->select;
	}else{
		$col="*";
	}
	if($this->where != ""){
		$where="where ".$this->where;
	}
	if($this->order != ""){
		$order="order by ".$this->order." DESC";
	}
	if($this->limit != ""){
		$limit="limit ".$this->limit;
	}	
	$sql="select $col from $table $where $order $limit ";
	$this->query($sql);
}

public function where($str){
	if(is_array($str)){
		foreach($str as $k=>$v){
			$arr[]="$k $v";
		}
		$this->where=implode(" and ",$arr);
	}else{
		$this->where=$str;
	}
}

public function order($str){
	$this->order=$str;
}

public function limit($start,$max){
	$this->limit="$start, $max";
}


}